package gui;

import database.entity.ContactGroup;
import gui.component.PreloadedFont;
import java.awt.Color;
import java.awt.Component;
import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Tom
 */
public class UIContactGroup extends javax.swing.JDialog 
{
    LinkedList<String> names;
    DefaultListModel model;
           
    UIMain main;    
    public UIContactGroup(UIMain main) 
    {
        super(main, true);
        this.main=main;
        initComponents();
        
        initInterface();
        setLocationRelativeTo(null);
    }
    
    private void initInterface()
    {
        liGroup.setCellRenderer(new GroupRenderer());
        names = new LinkedList<>();
        //load groups and get contact count
        
        model = new DefaultListModel();
        for (ContactGroup ctg : main.user.contactgroup) 
        {                  
            model.addElement(ctg);
            names.add(ctg.groupName);
        }
        liGroup.setModel(model);
    }

   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        liGroup = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtGroupName = new javax.swing.JTextField();
        btAdd = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        liGroup.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(liGroup);

        jLabel1.setText("Les groupes de contact actuels");

        jLabel2.setText("Nom du nouveau groupe");

        btAdd.setText("Ajouter");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btDelete.setText("Supprimer");
        btDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtGroupName, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 135, Short.MAX_VALUE)))))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(btDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtGroupName, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btAdd, btDelete});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        String groupName = txtGroupName.getText().trim();
        if (names.contains(groupName))
        {
            JOptionPane.showMessageDialog(this, "Ce nom a déjà apartenu à un autre groupe!", "", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
             //add new group to database
            ContactGroup ctg = main.user.databaseWorker.addGroup(groupName);
            
            //add to list
            model.addElement(ctg);
            names.add(groupName);
            
            //update UI
            txtGroupName.setText("");
            //refresh reference
            main.user.databaseWorker.getAllContactGroup(true);
            //reload model            
            main.reloadContactGroupModel();
            main.loadContactMode();
            JOptionPane.showMessageDialog(this, "Nouveau groupe ajouté", "", JOptionPane.INFORMATION_MESSAGE);
            
        }
    }//GEN-LAST:event_btAddActionPerformed

    private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteActionPerformed
        ContactGroup gi = (ContactGroup)liGroup.getSelectedValue();
        if (gi==null)
        {
            JOptionPane.showMessageDialog(this, "Choisissez un groupe pour supprimer!","",JOptionPane.ERROR_MESSAGE);            
        }
        else if ("Other".equals(gi.groupName))
        {
            JOptionPane.showMessageDialog(this, "Vous ne pouvez pas supprimer ce groupe par default!","",JOptionPane.ERROR_MESSAGE);  
        }
        else
        {
            int contactCount = gi.contacts!=null? gi.contacts.length:0;
            String message = contactCount>0? "Supprimer ce groupe va conduire à la suppression de " + contactCount + "contact(s)":"Vous êtes sur(e) pour la supression?";
            int result=JOptionPane.showConfirmDialog(this, message, "Confirmation de la suppression", JOptionPane.YES_NO_OPTION);
            if (result==JOptionPane.YES_OPTION)
            {
                //remove this group info
                model.removeElement(gi);
                names.remove(gi.groupName);
                
                //remove this group from database
                main.user.databaseWorker.deleteGroup(gi.groupID);
                
                //refresh reference
                main.user.databaseWorker.getAllContactGroup(true);
                main.reloadContactGroupModel();            
                main.loadContactMode();         
                
                JOptionPane.showMessageDialog(this, "Groupe supprime!","",JOptionPane.INFORMATION_MESSAGE);  
            }
        }
    }//GEN-LAST:event_btDeleteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btDelete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList liGroup;
    private javax.swing.JTextField txtGroupName;
    // End of variables declaration//GEN-END:variables
    
    final class GroupRenderer implements ListCellRenderer<ContactGroup>
    {        
        @Override
        public Component getListCellRendererComponent(JList<? extends ContactGroup> jlist, ContactGroup e, int i, boolean selected, boolean bln1)
        {
            JLabel lbGroup = new JLabel(e.groupName + " ("+ (e.contacts!=null? e.contacts.length:0)+")");
            lbGroup.setOpaque(true);
            lbGroup.setFont(PreloadedFont.CALIBRI_BOLD_20);
            if (selected)
            {
                lbGroup.setBackground(Color.LIGHT_GRAY);
            }
            else
            {
                lbGroup.setBackground(Color.WHITE);
            }
            return lbGroup;
        }        
    }
}
